﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang.BLL
{
    public class UserRepository
    {
        private QLGPMBEntities data = new QLGPMBEntities();

        public User GetByUserNameAndPassWord(string loginName, string password)
        {
            return data.Users.FirstOrDefault(u => u.UserName == loginName && u.Password == password && !u.isDisable);
        }

        public IEnumerable<User> GetAll(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = from p in data.Users
                          select p;
            totalRow = results.Count();
            return results.OrderBy(p=>p.UserName).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        public IEnumerable<User> GetAll()
        {
            var results = from p in data.Users
                          select p;
            return results.OrderBy(p => p.UserName);
        }

        public User GetByID(int ID)
        {
            var result = from p in data.Users
                         where p.UserID == ID
                         select p;
            return result.FirstOrDefault();
        }

        public User GetByID(int? ID)
        {
            return data.Users.FirstOrDefault(u => u.UserID == ID);
        }

        public User GetByUserName(string username)
        {
            return data.Users.FirstOrDefault(u => u.UserName == username);
        }

        public IEnumerable<User> getUserisDisable()
        {
            var results = from p in data.Users
                          where p.isDisable == true
                          select p;
            return results.OrderBy(p => p.UserName);
        }

        public IEnumerable<User> getUserisEnable()
        {
            var results = from p in data.Users
                          where p.isDisable == false
                          select p;
            return results.OrderBy(p => p.UserName);
        }

        public IEnumerable<User> GetByRoles(int roleID)
        {
            return data.Users.Where(p => data.UserRoles.Any(q => q.Role.RoleID == roleID));
        }

        public void ChangePassword(User u, string newpass, string oldpass)
        {
            if (u.Password == oldpass)
            {
                u.Password = newpass;
            }
            data.SaveChanges();
        }

        public void Insert(User u)
        {
            data.AddToUsers(u);
        }

        public void isDisable(int userID, bool disable)
        {
            User objU = GetByID(userID);
            objU.isDisable = disable;
            data.SaveChanges();
        }


        public bool isExists(string username)
        {
            return data.Users.Any(p => p.UserName.ToUpper() == username.ToUpper());
        }

        public void DeleteByID(int id)
        {
            var obj = GetByID(id);
            if (obj != null && obj.UserName != "admin")
            {
                data.DeleteObject(obj);
            }
        }

        public void Save()
        {
            data.SaveChanges();
        }
    }
}